package org.example.webmagic;

import org.example.dao.CompanyMapper;
import org.example.pojo.Company;
import org.springframework.beans.factory.annotation.Autowired;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DataPipe implements Pipeline {
    @Autowired
    CompanyMapper companyMapper;

    @Override
    public void process(ResultItems resultItems, Task task) {
        System.out.println();
        Map<String,Object> itemsAll = resultItems.getAll();
        Iterator<Map.Entry<String,Object>> iterator = itemsAll.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<String,Object> entry = iterator.next();
            if("company".equals(entry.getKey())){
                List<Company> companyList = (List<Company>) entry.getValue();
                if(companyList.size() < 1) return;

                for(Company company:companyList){
//                    System.out.println(">>>>>>"+company);
                    //插入数据库
                companyMapper.addCompany(company);
                }
            }
        }
    }
}
